package com.booksys.examination.controller;

import com.booksys.examination.entity.BookInfo;
import com.booksys.examination.entity.PageBean;
import com.booksys.examination.pagesize.Constant;
import com.booksys.examination.service.BookInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Administrator
 * @date 2019-08-13 15:54
 */
@Controller
public class BookInfoController {
    @Resource
    private BookInfoService bookInfoService;
   @GetMapping("/bookInfos")
    public String showBookList(Model model){
       List<BookInfo> bookInfos = bookInfoService.showAll();
       model.addAttribute("bookInfos",bookInfos);
        return "bookList";
   }
    @GetMapping("/pagination")
   public String showBookByPage(Integer typeId,String title,Integer isBorrow,Integer currentPage, Model model){
        if (StringUtils.isEmpty(currentPage))
            currentPage=1;
        if (StringUtils.isEmpty(typeId))
            typeId=0;
        if (StringUtils.isEmpty(title))
            title=null;
        if (StringUtils.isEmpty(isBorrow))
            isBorrow=-1;
        PageBean<BookInfo> pageBean = bookInfoService.doPagination(typeId,title,isBorrow,currentPage, Constant.NUM);
        model.addAttribute("pageBean",pageBean);
        model.addAttribute("typeId",typeId);
        model.addAttribute("isBorrow",isBorrow);
        model.addAttribute("title",title);
        return "bookList";
    }

    @GetMapping("/pagination/{typeId}/{title}/{isBorrow}/{currentPage}")
    public String showPage(Integer typeId,String title,Integer isBorrow,Integer currentPage, Model model){
        if (StringUtils.isEmpty(currentPage))
            currentPage=1;
        if (StringUtils.isEmpty(typeId))
            typeId=0;
        if (StringUtils.isEmpty(title))
            title=null;
        if (StringUtils.isEmpty(isBorrow))
            isBorrow=-1;
        PageBean<BookInfo> pageBean = bookInfoService.doPagination(typeId,title,isBorrow,currentPage, Constant.NUM);
        model.addAttribute("pageBean",pageBean);
        return "bookList";
    }
}
